<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Manage Forums" %>

<%@ Import Namespace="ClubStarterKit.Data" %>
<script runat="server">
#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)
        If String.IsNullOrEmpty(e.Values("Topic").ToString()) Then
            e.Values("Topic") = "Untitled Forum"
        End If
        If String.IsNullOrEmpty(e.Values("Description").ToString()) Then
            e.Values("Description") = "Forum Description"
        End If
        
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
        If String.IsNullOrEmpty(e.NewValues("topic").ToString()) Then
            e.NewValues("topic") = "Untitled Forum"
        End If
        If String.IsNullOrEmpty(e.NewValues("description").ToString()) Then
            e.NewValues("description") = "Forum Description"
        End If
    End Sub
    
    Public Shared Function GetUserName(ByVal memkey As String) As String
        Dim mem1 As New AspnetUsers(AspnetUsers.Columns.UserId, memkey)
        Dim un As String
        Try
            un = ClubStarterKit.Web.Members.FirstAndLastName(mem1.UserId)
        Catch ex As Exception
            un = ""
        End Try
        Return un
    End Function

    Protected Sub AddTopic_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClubStarterKit.Web.Forum.AddTopic(TopicName.Text, Description.Text, SectionCHECK.Checked, Integer.Parse(Weight.Text), LockedCHECK.Checked, VisibleCHECK.Checked, DropDownList1.SelectedValue)
        Response.Redirect("~/forum/manage.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Request("editid") Is Nothing Then
            AddTopic.Visible = True
            UpdateTopic.Visible = False
        Else
            AddTopic.Visible = False
            UpdateTopic.Visible = True
            Dim topic As New Topics(Topics.Columns.Topicid, Request("editid"))
            TopicName.Text = topic.Topic
            Description.Text = topic.Description
            SectionCHECK.Checked = topic.Section
            VisibleCHECK.Checked = topic.Visible
            LockedCHECK.Checked = topic.Locked
            Weight.Text = topic.Weight.ToString()
            DropDownList1.SelectedValue = topic.Moderatorkey.ToString()
        End If
    End Sub

    Protected Sub UpdateTopic_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClubStarterKit.Web.Forum.UpdateTopic(CStr(Request("editid")), TopicName.Text, Description.Text, SectionCHECK.Checked, Integer.Parse(Weight.Text), LockedCHECK.Checked, VisibleCHECK.Checked, DropDownList1.SelectedValue)
        Response.Redirect("~/forum/manage.aspx")
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
    
        <div class="fullwidth">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <h2>
                Forum Manager</h2>
            <br />
            <br />
            <asp:UpdatePanel ID="up1" runat="server">
            <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="TopicID" DataSourceID="ObjectDataSource1" Width="100%" OnRowUpdating="GridView1_RowUpdating" GridLines="None">
                <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="topicid" DataNavigateUrlFormatString="manage.aspx?editid={0}"
                        HeaderText="Edit" Text="Edit" />
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="False" />
                    <asp:BoundField DataField="topic" HeaderText="Forum" SortExpression="Topic" />
                    <asp:BoundField DataField="description" HeaderText="Description" SortExpression="Description" />
                    <asp:CheckBoxField DataField="section" HeaderText="Section" SortExpression="Section" />
                    <asp:CheckBoxField DataField="visible" HeaderText="Visible" SortExpression="Visible" />
                    <asp:CheckBoxField DataField="locked" HeaderText="Locked" SortExpression="Locked" />
                    <asp:BoundField DataField="weight" HeaderText="Weight" SortExpression="Weight" />
                    <asp:TemplateField HeaderText="Moderator">
                        <ItemTemplate>
                            <asp:Label ID="modname" runat="server" Text='<%# GetUserName(Eval("moderatorkey").ToString()) %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:HyperLinkField DataNavigateUrlFields="TopicID" DataNavigateUrlFormatString="Threads.aspx?to={0}"
                        HeaderText="Jump" Text="&#187;&#187;&#187;" />
                </Columns>
            </asp:GridView>
            </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress ID="prog1" DisplayAfter="200" AssociatedUpdatePanelID="up1" runat="server">
            <ProgressTemplate>
                <asp:Image ID="loading2" runat="server" SkinID="loading" />
            </ProgressTemplate>
            </asp:UpdateProgress>
        </div>
        <div id="columnleft">
            <a name="content_start" id="content_start"></a>
            <div class="leftblock">
                <ul>
                    <li>Section determines whether the item is a section heading or forum.</li><li>Weight
                        determines the ordering of items (starting with 1). Use increments of 10 for convenience. </li>
                    <li>Items of the same weight are sorted by name. </li>
                </ul>
            </div>
        </div>

        <div id="columnright">
            <div class="rightblock">
                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteTopic"
                    SelectMethod="GetTopics" TypeName="ClubStarterKit.Web.Forum"
                    UpdateMethod="UpdateTopic">
                    <DeleteParameters>
                        <asp:Parameter Name="TopicID" Type="Int32" />
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="TopicID" Type="String" />
                        <asp:Parameter Name="Topic" Type="String" />
                        <asp:Parameter Name="Description" Type="String" />
                        <asp:Parameter Name="Section" Type="Boolean" />
                        <asp:Parameter Name="Weight" Type="Int32" />
                        <asp:Parameter Name="Locked" Type="Boolean" />
                        <asp:Parameter Name="Visible" Type="Boolean" />
                    </UpdateParameters>
                </asp:ObjectDataSource>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                <br />
                <table>
                    <tr>
                        <td style="width: 100px">
                            Topic Name</td>
                        <td style="width: 100px">
                <asp:TextBox ID="TopicName" runat="server" Width="200px"></asp:TextBox></td>
                        <td style="width: 100px">
                    <asp:RequiredFieldValidator ControlToValidate="TopicName" ValidationGroup="Insert"
                    ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name required">*</asp:RequiredFieldValidator></td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Description</td>
                        <td style="width: 100px">
                <asp:TextBox ID="Description" runat="server" Text='<%# Bind("Description") %>' Height="67px" Width="200px"></asp:TextBox></td>
                        <td style="width: 100px">
                                <asp:RequiredFieldValidator ControlToValidate="Description" ValidationGroup="Insert"
                                    ID="RequiredFieldValidator2" runat="server" ErrorMessage="Description required">*</asp:RequiredFieldValidator></td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Section</td>
                        <td style="width: 100px">
                <asp:CheckBox ID="SectionCHECK" runat="server" /></td>
                        <td style="width: 100px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Visible</td>
                        <td style="width: 100px">
                <asp:CheckBox ID="VisibleCHECK" runat="server" Checked="True" /></td>
                        <td style="width: 100px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Locked</td>
                        <td style="width: 100px">
                <asp:CheckBox ID="LockedCHECK" runat="server" /></td>
                        <td style="width: 100px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Weight</td>
                        <td style="width: 100px">
                <asp:TextBox ID="Weight" runat="server"></asp:TextBox></td>
                        <td style="width: 100px">
                                <asp:RequiredFieldValidator ControlToValidate="Weight" ValidationGroup="Insert"
                                    ID="RequiredFieldValidator3" runat="server" ErrorMessage="Number Required">*</asp:RequiredFieldValidator>
                                <asp:RangeValidator Type="Integer" MinimumValue="0" MaximumValue="65000" ControlToValidate="Weight"
                                    ValidationGroup="Insert" ID="RangeValidator1" runat="server" ErrorMessage="Enter a Number">Enter a Number</asp:RangeValidator></td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Moderator</td>
                        <td style="width: 100px">
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2"
                                DataTextField="displayname" DataValueField="memkey">
                            </asp:DropDownList></td>
                        <td style="width: 100px">
                            <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="ListModerators"
                                TypeName="ClubStarterKit.Web.Members"></asp:ObjectDataSource>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                        </td>
                        <td style="width: 100px">
                <asp:Button id="AddTopic" runat="server" Text="Add Topic" OnClick="AddTopic_Click" />
                <asp:Button ID="UpdateTopic" runat="server" Text="Update Topic" OnClick="UpdateTopic_Click" /></td>
                        <td style="width: 100px">
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</asp:Content>
